//
//  VCPictureWall.m
//  MOSAD_HW2_1
//
//  Created by Jason on 2020/10/29.
//  Copyright © 2020 Jason. All rights reserved.
//

#import "VCPictureWall.h"

@interface VCPictureWall ()

@end

@implementation VCPictureWall

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"图库";
    self.view.backgroundColor = [UIColor blackColor];
    
    _pictureCount = 0;
    
    //    //设置返回控件
    //    UIBarButtonItem * leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"<返回" style:UIBarButtonItemStyleDone target:self action:@selector(pressBack)];
    //    //将导航元素项的左侧按钮赋值
    //    self.navigationItem.leftBarButtonItem = leftBtn;
    
    
    //设置状态栏为不透明
    self.navigationController.navigationBar.translucent = NO;
    
    UIScrollView * sv = [[UIScrollView alloc] init];
    sv.frame = CGRectMake(10, 10, self.view.bounds.size.width-20, self.view.bounds.size.height-20);
    //设置画布大小
    sv.contentSize = CGSizeMake(self.view.bounds.size.width-20, +130*9);
    //不显示栏
    sv.showsVerticalScrollIndicator = NO;
    //打开交互事件
    sv.userInteractionEnabled = YES;
    
    //初始化被选中的图片s序列号数组
    for(int i = 0; i < 5; i++) {
        _pictureNum[i] = 0;
    }
    count = 0;
    
    //导入要选择的图片
    for(int i = 0; i < 25; i++) {
        
        _isChosed[i] = NO;
        
        NSString * strName = [NSString stringWithFormat:@"%d.jpg", i+1];
        UIImage * image = [UIImage imageNamed:strName];
        UIImageView * iView = [[UIImageView alloc] initWithImage:image];
        iView.frame = CGRectMake(3+(i%4)*100, 10+(i/4)*140, 90, 130);
        [sv addSubview:iView];
        
        //开启交互模式
        iView.userInteractionEnabled = YES;
        //创建点击手势
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressTap:)];
        //单次点击
        tap.numberOfTapsRequired = 1;
        //单个手指
        tap.numberOfTouchesRequired = 1;
        
        [iView addGestureRecognizer:tap];
        
        
        //点击Label时设置为已选第几张图片
        UILabel * num = [[UILabel alloc] initWithFrame:CGRectMake(iView.bounds.size.width-30, 0, 30, 30)];
        num.backgroundColor = [UIColor whiteColor];
        num.font = [UIFont systemFontOfSize:17];
        num.textColor = [UIColor greenColor];
        num.textAlignment = NSTextAlignmentCenter;
        //将UILabel设置为圆形
        num.layer.cornerRadius = num.bounds.size.width/2;
        num.layer.masksToBounds = YES;
        num.clipsToBounds = YES;
        //添加UILabel的边框
        num.layer.borderWidth = 0.8;
        num.layer.borderColor = [[UIColor grayColor] CGColor];
        
        //开启交互模式
        num.userInteractionEnabled = YES;
        //创建点击手势
        UITapGestureRecognizer * tapNum = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressTapAddNum:)];
        //单次点击
        tapNum.numberOfTapsRequired = 1;
        //单个手指
        tapNum.numberOfTouchesRequired = 1;
        
        [num addGestureRecognizer:tapNum];
        
        num.tag = 101 + i;
        
        [iView addSubview:num];
        
        //        //点击Button时设置为已选第几张图片
        //        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(iView.bounds.size.width-30, 0, 30, 30)];
        //        btn.backgroundColor = [UIColor colorWithRed:128/255 green:128/255 blue:128/255 alpha:0.7];
        //        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //        btn.titleLabel.font = [UIFont systemFontOfSize:17];
        //        //默认文字居中
        //        //btn.contentHorizontalAlignment = NSTextAlignmentCenter;
        //        //将UILabel设置为圆形
        //        btn.layer.cornerRadius = btn.bounds.size.width/2;
        //        //设置超过子层的部分裁剪掉
        //        btn.layer.masksToBounds = NO;
        //        btn.clipsToBounds = YES;
        //        [btn addTarget:self action:@selector(pressAdd:) forControlEvents:UIControlEventTouchUpInside];
        //
        //        [iView addSubview:btn];
        
    }
    [self.view addSubview:sv];
    
    //设置导出按钮
    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"导出" style:UIBarButtonItemStylePlain target:self action:@selector(pressPublish)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
}

//-(void) pressBack {
//    NSLog(@"返回上级");
//    //将当前的视图控制器弹出，返回到上一级界面
//    //栈的弹出
//    [self.navigationController popViewControllerAnimated:YES];
//}


-(void) pressTap:(UITapGestureRecognizer *) tap {
    UIImageView * imageView = (UIImageView *)tap.view;
    //创建显示视图控制器
    VCImageShow * imageShow = [[VCImageShow alloc] init];
    
    imageShow.image = imageView.image;
    //将控制器推出
    [self.navigationController pushViewController:imageShow animated:YES];
    
}

-(void) pressTapAddNum:(UITapGestureRecognizer *) tap
{
    UILabel * label = (UILabel *)tap.view;
    
    if(_isChosed[label.tag - 101] == YES) { //判断是否已被u选中
        _pictureCount--;
        label.text = @"";
        _isChosed[label.tag - 101] = NO;
        _pictureNum[count--] = 0;
    }
    else {
        if(_pictureCount < 5) {
            label.text = @"✔";
            _isChosed[label.tag - 101] = YES;
            _pictureCount++;
            _pictureNum[count++] = label.tag - 100;
        }
        else if(_pictureCount >= 5) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"图片不能超过5张！" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction * OK = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
            [alertController addAction:OK];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }
    
}


-(void) pressPublish {
    //代理对象调用事件函数
    [_delegate getPictureNum:_pictureNum getPictureCount:_pictureCount];
    NSLog(@"传参");
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
